package com.dayaokeji.rhythmschoolstudent.client.home.course;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity;
import com.dayaokeji.rhythmschoolstudent.client.home.course.adapter.StudentListAdapter;
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.Page;
import com.dayaokeji.server_api.ServerResponse;
import com.dayaokeji.server_api.api.CourseApi;
import com.dayaokeji.server_api.domain.Member;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

/**
 * 学生管理
 */
public class StudentManagementActivity extends BaseActivity {

    public static final String EXTRA_COURSE_ID = "course_id";

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_student_list)
    RecyclerView rvStudentList;

    private int courseId;
    private static final CourseApi courseApi = ApiUtils.getApi(CourseApi.class);
    private Call<ServerResponse<List<Member>>> studentListCall;
    private StudentListAdapter studentListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_management);
        setSupportActionBar(toolbar);

        courseId = getIntent().getIntExtra(EXTRA_COURSE_ID,-1);

        if (courseId == -1) {
            ToastUtils.warning(R.string.data_incorrect);
            return;
        }

        init();
    }

    private void init() {
        setRecyclerView();
        requestStudentList();
    }


    private void setRecyclerView() {
        rvStudentList.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .marginResId(R.dimen.default_divider_height)
                .colorResId(R.color.color_default_divider)
                .build());
        studentListAdapter = new StudentListAdapter();
        rvStudentList.setAdapter(studentListAdapter);
        studentListAdapter.setEmptyView(R.layout.empty_layout,rvStudentList);
    }

    /**
     * request data
     */
    private void requestStudentList() {
        studentListCall = courseApi.member(courseId);
        studentListCall.enqueue(new ServerCallback<List<Member>>() {
            @Override
            public void onResponse(boolean success, ServerResponse<List<Member>> response) {
                if (success) {
                    if (studentListAdapter != null) {
                        studentListAdapter.setNewData(response.getBody());
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (studentListCall != null) {
            studentListCall.cancel();
        }
        super.onDestroy();
    }
}
